<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="生产日期" prop="scrq">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.scrq"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="报表名称" prop="bbmc">
        <el-input
          v-model="queryParams.bbmc"
          placeholder="请输入报表名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>  -->
      <!--  <el-form-item label="报表编号" prop="bbbh">
        <el-input
          v-model="queryParams.bbbh"
          placeholder="请输入报表编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="所属基地" prop="jd">
        <el-select
          v-model="queryParams.jd"
          placeholder="请选择"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in factoryListOptions"
            :key="dict.deptId"
            :label="dict.name"
            :value="dict.name"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['analysis_Product:analysisProduct:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['analysis_Product:analysisProduct:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['analysis_Product:analysisProduct:remove']"
          >删除</el-button
        >
      </el-col>
      <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['analysis_Product:analysisProduct:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="analysisProductList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="100px" />
      <el-table-column label="生产日期" align="center" prop="scrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报表名称" align="center" prop="bbmc" />
      <el-table-column label="报表编号" align="center" prop="bbbh" />
      <el-table-column label="制成合格率" align="center" prop="rate" />
      <el-table-column label="所属基地" align="center" prop="jd" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="250px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
            >导出</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['analysis_Product:analysisProduct:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['analysis_Product:analysisProduct:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仰口生产报对话框 -->
    <!--  <el-dialog fullscreen :title="title" :visible.sync="open" width="900px" append-to-body> -->
    <el-dialog
      fullscreen
      :title="title"
      :visible.sync="open"
      width="900px"
      append-to-body
      custom-class="myDialog"
    >
      <TableHeader
        @nameChange="nameChange"
        @nameChange2="nameChange2"
        :data="{
          mainTitle: '仰口生产报表',
          company: form.jd,
          code: form.bbbh,
        }"
      />
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="scrq">
              <el-date-picker
                :editable="false"
                clearable
                size="small"
                v-model="form.scrq"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择生产日期"
                :disabled="openDisabled"
                @change="emptyArr"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制成合格率" prop="rate">
              <el-input
                v-model="form.rate"
                placeholder="请输入制成合格率"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <!--  <el-col :span="12">
                <el-form-item label="报表编号" prop="bbbh">
                  <el-input v-model="form.bbbh" placeholder="请输入报表编号" :disabled="openDisabled" />
                </el-form-item>
            </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="所属基地" prop="jd">
              <el-select
                v-model="form.jd"
                placeholder="请选择"
                clearable
                size="small"
              >
                <el-option
                  v-for="dict in factoryListOptions"
                  :key="dict.deptId"
                  :label="dict.name"
                  :value="dict.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <!--  <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="openDisabled" />
                </el-form-item>
            </el-col> -->
        </el-row>
        <br />
        <el-divider content-position="center">仰口生产子信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddRptAnalysisProductSub"
              :disabled="openDisabled"
              >生成</el-button
            >
          </el-col>

          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleRate"
              :disabled="openDisabled"
              >制程合格率统计</el-button
            >
          </el-col>

          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteRptAnalysisProductSub"
              :disabled="openDisabled"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="rptAnalysisProductSubList"
          :row-class-name="rowRptAnalysisProductSubIndex"
          @selection-change="handleRptAnalysisProductSubSelectionChange"
          ref="rptAnalysisProductSub"
        >
          <el-table-column type="selection" width="50" align="center" fixed />
          <el-table-column
            fixed
            label="序号"
            align="center"
            prop="index"
            minWidth="50"
          />
          <!-- <el-table-column label="品种规格" prop="gg">
            <template slot-scope="scope">
                <el-input v-model="scope.row.gg" placeholder="请输入品种规格" :disabled="openDisabled"/>
            </template>
          </el-table-column> -->
          <!-- <el-table-column label="品种规格" prop="ggname">
            <template slot-scope="scope">
                <el-input v-model="scope.row.ggname" placeholder="请输入品种规格" :disabled="openDisabled"/>
            </template>
          </el-table-column> -->

          <el-table-column label="品种规格" prop="gg" minWidth="320" fixed>
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.ggname"
                placeholder="请输入规格"
                :disabled="openDisabled"
              />
              <!-- <Dialog
                @dialogChange="dialogChange($event, scope)"
                :echoValue="scope.row.ggname"
                parameter="rows"
                value="id"
                label="name"
                :multiple="false"
                search="name/code"
                url="/basic/productInfo/listSelect"
                :column="['产成品编号/code', '产成品名称/name']"
                placeholder="请选择内容"
              /> -->
            </template>
          </el-table-column>

          <el-table-column minWidth="140px" label="箱、包" prop="xb">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.xb"
                placeholder="请输入"
                :disabled="openDisabled"
                @change="updateAll(scope, $event)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="吨" prop="ton">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.ton"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="生产时长" prop="scsc">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.scsc"
                placeholder="请输入"
                :disabled="openDisabled"
                @change="automatic1(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="产线工时" prop="cxgs">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.cxgs"
                :disabled="openDisabled"
                placeholder="请输入"
                @change="automatic2(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="生产线人数" prop="scxrs">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.scxrs"
                placeholder="请输入"
                :disabled="openDisabled"
                @change="automatic2(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="生产效率" prop="scxl">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.scxl"
                placeholder="请输入生产效率"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="产线人均小时产量"
            prop="cxrjcl"
            minWidth="140px"
          >
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.cxrjcl"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="瓶坯_实用" prop="ppSy">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.ppSy"
                placeholder="请输入"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="瓶坯_消耗率" prop="ppXh">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.ppXh"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="瓶盖_实用" prop="pgSy">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.pgSy"
                placeholder="请输入"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="瓶盖_消耗率" prop="pgXh">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.pgXh"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="商标_实用" prop="sbSy">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.sbSy"
                placeholder="请输入"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="商标_消耗率" prop="sbXh">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.sbXh"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="纸箱_实用" prop="zxSy">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.zxSy"
                placeholder="请输入"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="纸箱_消耗率" prop="zxXh">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.zxXh"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="水_实用" prop="waterSy">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.waterSy"
                placeholder="请输入"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="水_吨用量" prop="waterYl">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.waterYl"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="电_度" prop="electricSy">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.electricSy"
                placeholder="请输入"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="电_吨用量" prop="electricYl" minWidth="140px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.electricYl"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="气_度" prop="airSy">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.airSy"
                placeholder="请输入"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="气_吨用量" prop="airYl">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.airYl"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="燃气_实用" prop="gasSy">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.gasSy"
                placeholder="请输入"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="燃气_吨用量" prop="gasYl">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.gasYl"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="标煤能耗_吨新水"
            prop="coalSy"
            minWidth="140px"
          >
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.coalSy"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="标煤能耗_吨电力"
            prop="coalYl"
            minWidth="140px"
          >
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.coalYl"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column minWidth="140px" label="水利用率" prop="water">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.water"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <!-- <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAnalysisProduct,
  getAnalysisProduct,
  delAnalysisProduct,
  addAnalysisProduct,
  updateAnalysisProduct,
  listAnalysisProductCompute,
} from "@/api/report/analysis/analysisProduct";
import { listProductionmeterday } from "@/api/report/process/productionmeterday";
import { listRecord } from "@/api/produce/record/record";
import { listProductInfo } from "@/api/basic/productInfo/productInfo";
import { getFactoryList } from "@/api/report/complain/complain";
export default {
  name: "AnalysisProduct",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRptAnalysisProductSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仰口生产报表格数据
      analysisProductList: [],
      // 仰口生产子表格数据
      rptAnalysisProductSubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 所属工厂
      factoryListOptions: [],
      //查询生产记录数据
      pDateStart: null,
      a51: null,
      a512: null,
      //返回条数
      flagRows: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        scrq: null,
        scrq1: null,
        bbmc: null,
        bbbh: null,
        jd: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rate: [{ required: true, message: "不能为空" }],
      },
    };
  },
  created() {
    this.getList();
    getFactoryList().then((response) => {
      this.factoryListOptions = response.data;
    });
  },
  methods: {
    emptyArr() {
      this.rptAnalysisProductSubList = [];
    },
    //修改箱数其它更新
    updateAll(scope, event) {
      // let mlval = scope.row.ggname;
      // console.log(mlval.indexOf('×'));
      // mlval=mlval.substring(0,mlval.indexOf('×'));
      // console.log(mlval);

      // console.log(Number(scope.row.capacity ),scope.row.ggname.lastIndexOf(""));
      /**吨 */
      scope.row.ton = (
        ((Number(scope.row.capacity) / 1000) * Number(event) * 24) /
        1000
      ).toFixed(2);
      /* 生产效率 */
      let sc = scope.row.scsc;
      if (sc != null && sc != "" && sc != 0) {
        scope.row.scxl = (Number(event) / 750 / sc).toFixed(2);
      } else {
        scope.row.scxl = 0;
      }
      //瓶胚消耗

      scope.row.ppXh = (
        (Number(scope.row.pgSy) -
          Number(event) * Number(scope.row.dateNumber)) /
        (Number(event) * Number(scope.row.dateNumber))
      ).toFixed(4);
      //瓶盖消耗
      scope.row.pgXh = (
        (Number(scope.row.pgSy) -
          Number(event) * Number(scope.row.dateNumber)) /
        (Number(event) * Number(scope.row.dateNumber))
      ).toFixed(4);
      //商标消耗
      scope.row.sbXh = (
        (Number(scope.row.sbSy) -
          Number(event) * Number(scope.row.dateNumber)) /
        (Number(event) * Number(scope.row.dateNumber))
      ).toFixed(4);
      //纸箱实用
      scope.row.zxSy = event;

      //水用量
      scope.row.waterYl = (
        Number(scope.row.waterSy) / Number(scope.row.ton)
      ).toFixed(2);
      //电用量
      scope.row.electricYl = (
        Number(scope.row.electricSy) / Number(scope.row.ton)
      ).toFixed(2);
      //气用量
      scope.row.airYl = (
        Number(scope.row.airSy) / Number(scope.row.ton)
      ).toFixed(2);
      /* 标准 吨水 */
      scope.row.coalSy = (
        ((Number(scope.row.waterSy) - Number(scope.row.ton)) * 0.0857) /
        Number(scope.row.ton)
      ).toFixed(2);
      /*标准煤吨 电力*/
      scope.row.coalYl = (
        (Number(scope.row.electricSy) * 0.1229) /
        Number(scope.row.ton)
      ).toFixed(2);

      if (scope.row.waterSy != 0 && scope.row.waterSy != null) {
        scope.row.water = (
          Number(scope.row.ton) / Number(scope.row.waterSy)
        ).toFixed(2);
      } else {
        scope.row.water = 0;
      }
    },
    automatic1(scope) {
      //生产效率联动
      let scsc_temp = this.rptAnalysisProductSubList[scope.$index].scsc;
      let ton_temp = this.rptAnalysisProductSubList[scope.$index].ton;

      if (scsc_temp != null && scsc_temp != "" && scsc_temp != 0) {
        this.rptAnalysisProductSubList[scope.$index].scxl = (
          Number(this.rptAnalysisProductSubList[scope.$index].xb) /
          750 /
          scsc_temp
        ).toFixed(2);
      } else {
        this.rptAnalysisProductSubList[scope.$index].scxl = 0;
      }
    },
    // 产线工时、手动输入 有关的计算
    automatic2(scope) {
      let a = this.rptAnalysisProductSubList[scope.$index].cxgs;
      let b = this.rptAnalysisProductSubList[scope.$index].scxrs;
      if (a != null && a != "" && b != "" && b != null) {
        this.rptAnalysisProductSubList[scope.$index].cxrjcl = (
          this.rptAnalysisProductSubList[scope.$index].xb /
          (a * b)
        ).toFixed(2);
      }
    },
    //动态变更数据
    automatic3(scope) {
      // console.log(333, scope);
      let a = this.rptAnalysisProductSubList[scope.$index].zxSy;
      if (a != null && a != "") {
        this.rptAnalysisProductSubList[scope.$index].zxXh = (
          (scope.row.zxSy - scope.row.xb) /
          scope.row.xb
        ).toFixed(2);
      }
      //瓶胚消耗
      this.rptAnalysisProductSubList[scope.$index].ppXh = (
        (Number(scope.row.ppSy) - Number(scope.row.xb) * 24) /
        (Number(scope.row.xb) * 24)
      ).toFixed(4);

      //瓶盖消耗
      this.rptAnalysisProductSubList[scope.$index].pgXh = (
        (Number(scope.row.pgSy) - Number(scope.row.xb) * 24) /
        (Number(scope.row.xb) * 24)
      ).toFixed(4);

      //商标消耗
      this.rptAnalysisProductSubList[scope.$index].sbXh = (
        (Number(scope.row.sbSy) - Number(scope.row.xb) * 24) /
        (Number(scope.row.xb) * 24)
      ).toFixed(4);

      // let timeNum=0;
      // this.rptAnalysisProductSubList.forEach((item) => {
      //   // console.log(item);
      //     timeNum += parseFloat(item.scsc);
      //   });

      //水
      this.rptAnalysisProductSubList[scope.$index].waterYl = (
        Number(scope.row.waterSy) / Number(scope.row.ton)
      ).toFixed(2);
      // 电
      this.rptAnalysisProductSubList[scope.$index].electricYl = (
        Number(scope.row.electricSy) / Number(scope.row.ton)
      ).toFixed(2);
      // 气
      this.rptAnalysisProductSubList[scope.$index].airYl = (
        Number(scope.row.airSy) / Number(scope.row.ton)
      ).toFixed(2);

      //燃气
      let ton = scope.row.ton;
      if (ton != "" && ton != null) {
        this.rptAnalysisProductSubList[scope.$index].gasYl = (
          scope.row.gasSy / ton
        ).toFixed(2);
      }

      //水使用
      // let waterSy_temp = this.rptAnalysisProductSubList[scope.$index].waterSy;
      //电使用
      // let electricSy_temp = this.rptAnalysisProductSubList[scope.$index].electricSy;
      /* 标准 吨水 */
      this.rptAnalysisProductSubList[scope.$index].coalSy = (
        ((Number(scope.row.waterSy) - Number(scope.row.ton)) * 0.0857) /
        Number(scope.row.ton)
      ).toFixed(2);
      /*标准煤吨 电力*/
      this.rptAnalysisProductSubList[scope.$index].coalYl = (
        (Number(scope.row.electricSy) * 0.1229) /
        Number(scope.row.ton)
      ).toFixed(2);
    },
    /** 查询仰口生产报列表 */
    getList() {
      this.loading = true;
      listAnalysisProduct(this.queryParams).then((response) => {
        this.analysisProductList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    dialogChange(event, scope) {
      // console.log(scope);
      //  //选择后续操作
      // if (event.options instanceof Array == true) {
      //   this.rptProArrangesub1List.splice(scope.$index, 1, ...event.options);
      // } else {
      //   this.rptProArrangesub1List[scope.$index].gg = event.options;
      // }
      // this.rptProArrangesub1List.push({});
      // this.rptProArrangesub1List.pop();
      // console.log(event);
      //this.rptAnalysisProductSubList[scope.$index].gg = event.value;
      //this.rptAnalysisProductSubList[scope.$index].ggname = event.label;
      // console.log(this.rptProArrangesub1List)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        scrq: null,
        bbmc: "仰口生产报表",
        bbbh: "LK-QF-LS1-10-007-11-01",
        jd: "仰口生产基地",
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.rptAnalysisProductSubList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.scrq != null) {
        let arr = this.queryParams.scrq;
        this.queryParams.scrq = arr[0];
        this.queryParams.scrq1 = arr[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加仰口生产报表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getAnalysisProduct(id).then((response) => {
        this.form = response.data;
        this.rptAnalysisProductSubList =
          response.data.rptAnalysisProductSubList;
        this.open = true;
        this.title = "修改仰口生产报表";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getAnalysisProduct(id).then((response) => {
        this.form = response.data;
        this.rptAnalysisProductSubList =
          response.data.rptAnalysisProductSubList;
        this.open = true;
        this.title = "查看仰口生产报表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.rptAnalysisProductSubList = this.rptAnalysisProductSubList;
          if (this.form.id != null) {
            updateAnalysisProduct(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAnalysisProduct(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除仰口生产报表编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delAnalysisProduct(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 仰口生产子序号 */
    rowRptAnalysisProductSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 仰口生产子添加按钮操作 */
    handleAddRptAnalysisProductSub() {
      /* 计算总时长 */
      if (this.form.scrq == "" || this.form.scrq == null) {
        this.msgError("请选择生产日期");
      } else {
        listRecord({ pDateStart: this.form.scrq }).then((response) => {
          //查询生成记录表格获取所有的产成品记录 没有过滤掉其他基地
          this.flagRows = response.total;

          if (this.flagRows == 0) {
            this.msgError("未找到当天生产记录数据！");
            return;
          } else {
            listProductionmeterday({
              a51: this.form.scrq,
              a512: this.form.scrq,
            }).then((response) => {
              // 查询当天用电记录
              this.flagRows = response.total;
              if (this.flagRows == 0) {
                this.msgError("未找到当天用电记录数据！");
                return;
              } else {
                /* 计算总时长 */
                var timeNum = 0;
                listAnalysisProductCompute({ scrq: this.form.scrq }).then(
                  (res) => {
                    //src\main\java\com\huagao\report\analysis\controller\RptAnalysisProductController.java
                    // url: '/productionProcess/analysisProduct/compute',
                    //获取吹瓶机每日的参数。获取电表每日参数。电和气使用量，商标损耗。

                    if (res.rows.length == 0 || res.rows == null) {
                      this.msgError("未找到吹瓶机改日期记录");
                      return null;
                    }
                    res.rows.forEach((item) => {
                      timeNum += parseFloat(item.sjc);
                    });
                  }
                );

                listAnalysisProductCompute({ scrq: this.form.scrq }).then(
                  (res) => {
                    // console.log(res.rows);
                    res.rows.forEach((item) => {
                      // console.log("item", item);
                      var obj = new Object();
                      obj.gg = item.gg;
                      obj.ggname = item.ggname;
                      obj.xb = Number(item.xb);
                      obj.ton = null;
                      //自动计算
                      obj.cxrjcl = "";
                      obj.ppSy = "";
                      obj.ppXh = "";
                      obj.pgSy = "";
                      obj.pgXh = "";
                      obj.sbSy = "";
                      obj.sbXh = "";
                      obj.zxSy = "";
                      obj.zxXh = "";
                      obj.scsc = "";
                      obj.cxgs = "";
                      obj.scxrs = "";
                      /* 能源消耗率 */
                      obj.waterSy = "";
                      obj.waterYl = "";
                      obj.electricSy = "";
                      obj.electricYl = "";
                      obj.airSy = "";
                      obj.airYl = "";
                      //手动填写
                      obj.zxSy = Number(item.xb);
                      obj.zxXh = "";
                      obj.gasSy = "";
                      obj.gasYl = "";
                      obj.coalSy = "";
                      obj.coalYl = "";
                      obj.water = "";
                      obj.remark = "";
                      //动态切换计算参数
                      obj.capacity = 0;
                      obj.sjc = "";
                      obj.preformNum = 0;
                      obj.capNum1 = 0;
                      obj.useNum = 0;
                      obj.dateNumber = 0;
                      obj.ssy = 0;
                      obj.dsy = 0;
                      obj.qsy = 0;
                      obj.dsy = 0;
                      /* 查询规格 */
                      let date = null;
                      listProductInfo({ name: item.ggname }).then((resp) => {
                        // console.log(99,resp)
                        // console.log(resp);
                        date = resp.rows[0];
                        obj.capacity = date.capacity; //动态切换计算参数
                        // console.log(date);
                        /* 吨：公式计算=产量*品名前3个数字即容量*品名插号后2位数字即每箱或包的数量/100000  图中为例即：1947*330*24/100000=15.42（取2位小数）*/
                        obj.ton = (
                          ((date.capacity / 1000) * Number(item.xb) * 24) /
                          1000
                        ).toFixed(2);
                        // console.log(item);
                        obj.scsc = item.sjc;
                        /* 生产效率 */
                        let sc = item.sjc;
                        if (
                          sc != null &&
                          sc != "" &&
                          item.sjc != 0 &&
                          item.sjc != null
                        ) {
                          obj.scxl = (Number(item.xb) / 750 / item.sjc).toFixed(
                            2
                          );
                        } else {
                          obj.scxl = 0;
                        }
                        obj.sc = item.sjc; //动态切换计算参数
                        /* 原辅料消耗率 */
                        obj.dateNumber = date.number; //动态切换计算参数
                        //瓶胚使用
                        obj.ppSy = Number(item.preformNum).toFixed(0);
                        //瓶胚消耗
                        if (Number(item.xb) != 0) {
                          obj.ppXh = (
                            (Number(item.preformNum) -
                              Number(item.xb) * date.number) /
                            (Number(item.xb) * date.number)
                          ).toFixed(4);
                        }
                        obj.preformNum = Number(item.preformNum); //动态切换计算参数

                        //瓶盖使用
                        obj.pgSy = Number(item.capNum1).toFixed(0);
                        //瓶盖消耗
                        if (Number(item.xb) != 0) {
                          obj.pgXh = (
                            (Number(item.capNum1) -
                              Number(item.xb) * date.number) /
                            (Number(item.xb) * date.number)
                          ).toFixed(4);
                        }
                        obj.capNum1 = Number(item.capNum1); //动态切换计算参数

                        //商标使用
                        obj.sbSy = Number(item.useNum).toFixed(0);
                        //商标消耗
                        if (Number(item.xb) != 0) {
                          obj.sbXh = (
                            (Number(item.useNum) -
                              Number(item.xb) * date.number) /
                            (Number(item.xb) * date.number)
                          ).toFixed(4);
                        }
                        obj.useNum = Number(item.useNum); //动态切换计算参数
                        obj.zxSy = obj.xb;
                        //纸箱消耗率
                        obj.zxXh =
                          (Number(obj.zxSy) - Number(obj.xb)) / Number(obj.xb);
                        /* 能源消耗率 */
                        //水

                        // obj.waterSy = (Number(item.ssy)* item.sjc/timeNum).toFixed(1);
                        obj.waterSy = Number(item.ssy).toFixed(1);
                        //电
                        obj.electricSy = (
                          (Number(item.dsy) * item.sjc) /
                          timeNum
                        ).toFixed(1);
                        //气
                        obj.airSy = (
                          (Number(item.qsy) * item.sjc) /
                          timeNum
                        ).toFixed(1);

                        if (Number(obj.ton) != 0) {
                          obj.ssy = item.ssy; //动态切换计算参数
                          obj.dsy = item.dsy; //动态切换计算参数
                          obj.qsy = item.qsy; //动态切换计算参数
                          obj.dsy = item.dsy; //动态切换计算参数
                          //水吨用量
                          obj.waterYl = (
                            Number(item.ssy) / Number(obj.ton)
                          ).toFixed(2);
                          //电吨用量
                          obj.electricYl = (
                            obj.electricSy / Number(obj.ton)
                          ).toFixed(2);
                          //气用量
                          obj.airYl = (obj.airSy / Number(obj.ton)).toFixed(2);
                          /* 标准 水 */
                          obj.coalSy = (
                            ((obj.waterYl - Number(obj.ton)) * 0.0857) /
                            Number(obj.ton)
                          ).toFixed(2);
                          /*标准 电力*/
                          obj.coalYl = (
                            (obj.electricSy * 0.1229) /
                            Number(obj.ton)
                          ).toFixed(2);
                        } else {
                          obj.waterYl = 0;
                          obj.electricYl = 0;
                          obj.airYl = 0;
                          obj.coalSy = 0;
                          obj.coalYl = 0;
                        }
                        if (item.ssy != 0 && item.ssy != null) {
                          obj.water = (
                            Number(obj.ton) / Number(item.ssy)
                          ).toFixed(2);
                        } else {
                          obj.water = 0;
                        }
                      });

                      this.rptAnalysisProductSubList.push(obj);
                    });
                  }
                );
              }
            });
          }
        });
      }
    },
    /** 统计职称合格率 成品 / 瓶胚 **/
    handleRate() {
      let sum_xb = 0;
      let sum_ppsy = 0;
      for (let i_zc = 0; i_zc < this.rptAnalysisProductSubList.length; i_zc++) {
        let xb_zc = this.rptAnalysisProductSubList[i_zc].xb;
        let ppsy_zc = this.rptAnalysisProductSubList[i_zc].ppSy;
        sum_xb += Number(xb_zc);
        sum_ppsy += Number(ppsy_zc);
      }
      let resultDate = ((sum_xb * 24 * 100) / sum_ppsy).toFixed(2);
      this.$set(this.form, "rate", resultDate + "%");
    },
    /** 仰口生产子删除按钮操作 */
    handleDeleteRptAnalysisProductSub() {
      if (this.checkedRptAnalysisProductSub.length == 0) {
        this.$alert("请先选择要删除的仰口生产子数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.rptAnalysisProductSubList.splice(
          this.checkedRptAnalysisProductSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleRptAnalysisProductSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.rptAnalysisProductSub.clearSelection();
        this.$refs.rptAnalysisProductSub.toggleRowSelection(selection.pop());
      } else {
        this.checkedRptAnalysisProductSub = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "analysis_Product/analysisProduct/export",
        {
          ...this.queryParams,
        },
        "仰口生产报" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    excelhandleView(row) {
      this.download(
        "productionProcess/analysisProduct/exportexcel?id=" + row.id,
        {
          ...this.queryParams,
        },
        "仰口生产产出日报表" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    //接收编号
    nameChange(e) {
      this.form.bbbh = e;
    },
    nameChange2(e) {
      this.form.jd = e;
    },
  },
};
</script>
